package frame;

import util.MultimediaIO;

import type.GameMode;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
//整合并修复bug
/**
 *
 */
public class MainViewPanel extends JPanel {
    // 所属画框
    public MainFrame frame;
    private BufferedImage backgroundImage;
    private JButton startButton; // 开始游戏按钮
    private JButton rankButton;  // 排行榜按钮
    private JButton settingButton; // 游戏设置按钮
    private JButton introductionButton;  // 游戏说明按钮

    public MainViewPanel(MainFrame frame) {
        this.frame = frame;
        // 获取背景图片
        try {
            backgroundImage = ImageIO.read(new File("src/static/BackgroundPicture.png"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // 创建按钮
        startButton = new JButton("开始游戏");
        rankButton = new JButton("排行榜");
        settingButton = new JButton("游戏设置");
        introductionButton = new JButton("游戏说明");

        // 设置按钮坐标
        setLayout(null);
        startButton.setBounds(310, 250, 180, 40);
        rankButton.setBounds(310, 310, 180, 40);
        settingButton.setBounds(310, 370, 180, 40);
        introductionButton.setBounds(310, 430, 180, 40);

        // 添加监听事件
        startButton.addActionListener((e) -> {
            // 跳转到游戏界面
            String audioFilePath = "src/static/贪吃蛇大作战无限模式.wav";
            if (frame.isSoundCheckBox()) MultimediaIO.playAudio(audioFilePath);
            if(frame.GAME_MODE == GameMode.CLASSIC){
                GamePanel gamePanel = new GamePanel(frame);
                frame.setContentPane(gamePanel);
                gamePanel.requestFocusInWindow();
            }else if(frame.GAME_MODE == GameMode.ADVANCED){
                //进入拓展游戏界面
                GamePanel gamePanel = new GamePanel(frame);
                frame.setContentPane(gamePanel);
                gamePanel.requestFocusInWindow();
            }else{
                System.out.println("模式选择错误类型");
            }
            frame.revalidate(); // 重新绘制画框
        });

        rankButton.addActionListener((e) -> {
            // 跳转到排行榜界面
            RankPanel rankPanel = new RankPanel(this.frame);
            frame.setContentPane(rankPanel);
            rankPanel.requestFocusInWindow();
            frame.revalidate();
        });

        settingButton.addActionListener((e) -> {
            // 跳转到设置界面
            SettingPanel settingPanel = new SettingPanel(this.frame);
            frame.setContentPane(settingPanel);
            settingPanel.requestFocusInWindow();
            frame.revalidate();
        });

        introductionButton.addActionListener((e) -> {
            // 退出游戏
            IntroductionPanel introductionPanel = new IntroductionPanel(this.frame);
            frame.setContentPane(introductionPanel);
            introductionPanel.requestFocusInWindow();
            frame.revalidate();
        });

        add(startButton);
        add(rankButton);
        add(settingButton);
        add(introductionButton);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (backgroundImage != null) {
            g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
        }

        // 设置绘制字体和颜色
        Font font = new Font("黑体", Font.BOLD, 70);
        g.setFont(font);
        g.setColor(Color.BLACK);

        // 绘制文字
        String text = "贪吃蛇";
        int x = 295;
        int y = 180;
        g.drawString(text, x, y);
    }
}
